<?php
namespace app\api\controller;

use app\common\model\MealSpeciModel;
use think\App;
use app\common\model\MealModel;

/**  YSF
 *   套餐管理 API
 *   Date: 2018/12/12
 * Class Meal
 * @package app\api\controller
 */
class Meal extends Base
{
    protected $mealModel;
    protected $mealSpeciModel;

    // 初始化构造方法
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->mealModel = new MealModel();
        $this->mealSpeciModel = new MealSpeciModel();
    }

    // 套餐列表
    public function getMealAll()
    {
        // 接收参数
        $page = $this->parm['page'] ?? 1;
        $pageSize = $this->parm['pageSize'] ?? 10;

        $where['status'] = 1;  // 状态正常
        $where['is_del'] = 1;  // 未被删除
        $field = 'meal_id,name,type,image';
        $order = 'create_time DESC,meal_id DESC';
        // 套餐列表查询
        $data = $this->mealModel->getAll($where, $field, $page, $pageSize,[], $order);
        if ($data) {
            foreach ($data as $k => &$v)
            {
                $v['image_url'] = '';
                if ($v['image']) {
                    $v['image_url'] = config('api.qi_niu_url').$v['image'];
                }
            }
            return json(['code' => 1, 'message' => '获取成功', 'data' => $data]);
        }
            return json(['code' => 0, 'message' => '暂无套餐', 'data' => $data]);
    }

    // 套餐详情
    public function getMealDetail()
    {
        // 接收参数
        $meal_id = $this->parm['meal_id'] ?? '';
        if (empty($meal_id)) {
            return json(['code' => 0, '缺少必选参数', 'data' => '']);
        }
        $join = [
            ['set_meal_detail md', 'md.meal_id = a.meal_id', 'left']  // 套餐详情--套餐
        ];
        $where['a.meal_id'] = $meal_id;
        $field = 'a.meal_id,a.name,a.type,a.desc,a.image,md.meal_detail,md.service_detail';
        // 1-- 获取套餐详情
        $data = $this->mealModel->getOne($where, $field,$join);
        unset($where);
        if ($data) {
            $data['image'] = config('api.qi_niu_url').$data['image'];
            $where['meal_id']= $meal_id;
            $field = 'id,name,stock,sPrice,sales,stock';
            // 2-- 获取规格
            $data['speci'] = $this->mealSpeciModel->getAll($where, $field);
            unset($where);
            if ($data['speci']) {
                foreach ($data['speci'] as $k => &$v)
                {
                    $v['sPrice'] = formatMoney($v['sPrice']);
                    $v['isStock'] = 1; // 默认有库存
                    if ($v['sales'] >= $v['stock']) {
                        $v['isStock'] = 2; // 2-表示没有库存了
                    }
                }
            }
            returnJson(1, '获取成功', $data);
        }
            returnJson(0, '获取失败');
    }

    // 判断是否还有库存
    public function isStock()
    {
        // 接收参数
        $meal_id = $this->parm['meal_id'] ?? '';
        $speci_id = $this->parm['speci_id'] ?? '';
        if (empty($meal_id) || empty($speci_id)) {
            returnJson(-1001, '缺少参数');
        }
        // 查询库存
        $where['meal_id'] = $meal_id;
        $where['id'] = $speci_id;
        $meal_speci_data = $this->mealSpeciModel->getOne($where, 'sales,stock');
        unset($where);
        if ($meal_speci_data['sales'] >= $meal_speci_data['stock']) {
            returnJson(-1000, '没有库存了，请选择其他规格');
        }
            returnJson(1000, '还有库存');
    }





}